using System;
using System.Collections.Generic;
using System.Web.Security;

namespace SpiderMaster.AspNet.TradingSystem
{
    /// <summary> Holds Pretty Much the Entrie Memberhip Data;
    /// ProfileCommon, .Net Membership, </summary>
    [Serializable]
    [ReganSchroder(1, 10, 2009)]
    public sealed partial class TradingCenterMember
    {
        /// <summary> Emtpy.
        /// </summary>
        public static readonly TradingCenterMember Empty;

        /// <summary> Initializes the <see cref="TradingCenterMember"/> class.
        /// </summary>
        static TradingCenterMember() { Empty = new TradingCenterMember(); }

        private TradingCenterMember() : this(MemberManagement.DefaultValues.SecurityAnswerHidden) { }

        /// <summary> Initializes the <see cref="TradingCenterMember"/> class. </summary>
        /// <param name="SecurityAnswer">The security answer.</param>
        public TradingCenterMember(string SecurityAnswer)
        {
            this.SecurityAnswer = SecurityAnswer;
            this.Password = MemberManagement.DefaultValues.PasswordHidden;
            AspNetMembership = null;
            ListingCommon = TradingCenterMemberListingCommon.Empty;
            Profile = TradingCenterProfileCommon.Empty;
        }

        [ReganSchroder(5, 10, 2009)]
        private TradingCenterMember(int MemberId, string UserName)
        {
            // TODO:

            if (UserName.IsNotNullOrEmpty())
            {
                this.UserName = UserName;

                AspNetMembership = Membership.GetUser(UserName);
                Profile = TradingCenterProfileCommon.GetProfile(UserName);
                // ListingCommon = TradingCenterMemberListingCommon.GetMember(ProfileCommon.MemberId);
            }
            else if (MemberId.IsAtLeast(1))
            {
                //  ListingCommon = TradingCenterMemberListingCommon.GetMember(MemberId);
                Profile = TradingCenterProfileCommon.GetProfile(ListingCommon.UserName);
                AspNetMembership = Membership.GetUser(ListingCommon.UserName);

                this.UserName = AspNetMembership.UserName;
            }

            if (AspNetMembership.IsNotNull() && TradingCenterProfileCommon.IsNotNullOrEmpty(Profile))
                // TODO: && TradingCenterMemberListingCommon.IsNotNullOrEmpty(ListingCommon))
                MembershipCreated = true;
            else Clear();
            // TODO: Location = DynamicLocation.GetLocation(ProfileCommon.MasterLocationId);
        }

        private void Clear()
        {
            Profile = TradingCenterProfileCommon.Empty;
            UserName = string.Empty;
            AspNetMembership = null;
        }

        /// <summary> Gets the account balance. </summary>
        /// <value>The account balance.</value>
        [ReganSchroder(4, 10, 2009)] // TODO:
        public decimal AccountBalance { get { return 0.0M; } }

        /// <summary> Gets a value indicating whether this instance can modify roles. </summary>
        /// <value>
        /// 	<c>true</c> if this instance can modify roles; otherwise, <c>false</c>.
        /// </value>
        [ReganSchroder(5, 10, 2009)]
        public bool CanModifyRoles { get { return (IsAdministrator && IsInRole(PredefinedRole.RoleAdministrator)); } }

        /// <summary> Gets the errors. </summary>
        /// <value>The errors.</value>
        public List<string> Errors { get { return this.GetErrors(); } }

        /// <summary> Gets a value indicating whether this instance has errors. </summary>
        /// <value>
        /// 	<c>true</c> if this instance has errors; otherwise, <c>false</c>.
        /// </value>
        public bool HasErrors { get { return (Errors.Count > 0); } }

        /// <summary> Gets a value indicating whether this instance is administrator. </summary>
        /// <value>
        /// 	<c>true</c> if this instance is administrator; otherwise, <c>false</c>.
        /// </value>
        [ReganSchroder(5, 10, 2009)]
        public bool IsAdministrator { get { return IsInRole(PredefinedRole.GeneralAdministrator); } }

        /// <summary> When true this will show Passwords in forms etc.
        /// When false Passwords and other vital secure information is Hidden by Predefined Strings
        /// </summary>
        public bool SecurityEnabled { get; private set; }

        /// <summary> Gets or sets a value indicating whether [membership created]. </summary>
        /// <value><c>true</c> if [membership created]; otherwise, <c>false</c>.</value>
        public bool MembershipCreated { get; private set; }

        /// <summary> Gets or sets the profile common. </summary>
        /// <value>The profile common.</value>
        public TradingCenterProfileCommon Profile { get; set; }

        /// <summary> Gets or sets the ASP net membership. </summary>
        /// <value>The ASP net membership.</value>
        public MembershipUser AspNetMembership { get; set; }

        /// <summary> Gets or sets the listing common. </summary>
        /// <value>The listing common.</value>
        public TradingCenterMemberListingCommon ListingCommon { get; set; }

        // TODO:
        ///// <summary> Gets or sets the location. </summary>
        ///// <value>The location.</value>
        //public DynamicLocation Location { get; private set; }

        /// <summary> Gets or sets the password. </summary>
        /// <value>The password.</value>
        public string Password { get; private set; }

        #region Home Phone - To String

        public string HomePhoneToString
        {
            get
            {
                throw new NotImplementedException("Please correct this method");
                // TODO: return ProfileCommon.Residential.PhoneCountryCode + "-" + ProfileCommon.Residential.PhoneAreaCode + "-" + ProfileCommon.Residential.PhoneLocalNumber;
            }
        }

        #endregion

        #region Mobile Phone - To String

        public String MobilePhoneToString
        {
            get
            {
                throw new NotImplementedException("Please correct this method");
                // TODO: return ProfileCommon.MobilePhone.CountryCode + "-" + ProfileCommon.MobilePhone.AreaCode + "-" + ProfileCommon.MobilePhone.LocalNumber;
            }
        }

        #endregion

        #region Other Phone - To String

        public String OtherPhoneToString
        {
            get
            {
                throw new NotImplementedException("Please correct this method");
                // TODO: return ProfileCommon.OtherPhone.CountryCode + "-" + ProfileCommon.OtherPhone.AreaCode + "-" + ProfileCommon.OtherPhone.LocalNumber;
            }
        }

        #endregion

        /// <summary> Gets or sets the security answer. </summary>
        /// <value>The security answer.</value>
        public string SecurityAnswer { get; private set; }

        /// <summary> Gets the user name of the user. </summary>
        /// <value>The user name of the user.</value>
        [ReganSchroder(5, 10, 2009)]
        public string UserName { get; private set; }
    }
}